/*
 * Copyright © 2018 CODESTD.COM Inc. All rights reserved.
 */
package com.codestd.security.shiro;

import com.codestd.security.base.ResponseBean;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 使用response输出ResponseBean
 *
 * @author jaune
 * @since 1.0.0
 */
public final class ResponseUtils {

    // 阻止使用new
    private ResponseUtils() {
    }

    public static void printResponseBean(ServletResponse servletResponse, ResponseBean responseBean) {
        HttpServletResponse response = WebUtils.toHttp(servletResponse);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().print(responseBean.asJson());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void printAccessDeniedError(ServletResponse response) {
        ResponseBean responseBean = ResponseBean.builder()
                .success(false)
                .status(401)
                .message("权限不足")
                .build();

        printResponseBean(response, responseBean);
    }
}
